﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IOEntity;
using IOBusiness;
namespace Indochinaodyssey.Admin
{
    public partial class News : System.Web.UI.Page
    {
        clsBSNews obj = new clsBSNews();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                if (Request.QueryString["action"] != null)
                {
                    if (this.pnInfo.Visible == false)
                    {
                        if (Request.QueryString["action"].ToString().Equals("new"))
                        {
                            this.pnInfo.Visible = true;
                            this.txtTieuDe.Text = "";
                            this.imgHinhAnh.ImageUrl = "";
                            this.txtGhiChu.Text = "";
                            this.txtNoiDung.Value = "";
                            this.chbTinHot.Checked = false;
                        }
                        else
                        {
                            if (Request.QueryString["action"].ToString().Equals("edit"))
                            {
                                if (Request.QueryString["ID"] != null)
                                {
                                    this.pnInfo.Visible = true;
                                    int id = Convert.ToInt32(Request.QueryString["ID"]);
                                    clsNews news = new clsNews();
                                    news = obj.GetById(id);
                                    this.txtTieuDe.Text = news.TieuDe;
                                    this.txtGhiChu.Text = news.GhiChu;
                                    this.txtNoiDung.Value = news.NoiDung;
                                    this.chbTinHot.Checked = news.Hot;
                                    this.imgHinhAnh.ImageUrl = "~/Images/HinhAnh/" + news.HinhAnh;
                                    this.hfHinhAnh.Value = news.HinhAnh;
                                    this.ddlCateNew.SelectedValue = news.CateNew.Id.ToString();
                                }
                            }
                        }
                    }
                }
                else
                {
                    this.pnInfo.Visible = false;
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["action"] != null)
                {
                    clsNews news = new clsNews();
                    news.TieuDe = this.txtTieuDe.Text;
                    news.HinhAnh = hfHinhAnh.Value;
                    news.CateNew = new clsCateNew();
                    news.CateNew.Id = Convert.ToInt32(this.ddlCateNew.SelectedValue);
                    news.GhiChu = this.txtGhiChu.Text;
                    news.NoiDung = this.txtNoiDung.Value;
                    news.Hot = this.chbTinHot.Checked;
                    string filename = "";
                    if (Request.QueryString["action"].ToString().Equals("new"))
                    {
                        if (this.fuFile.HasFile)
                        {
                            filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/"));
                            news.HinhAnh = filename;
                        }
                        obj.Insert(news);
                        this.txtTieuDe.Text = "";
                        this.imgHinhAnh.ImageUrl = "";
                        this.txtGhiChu.Text = "";
                        this.txtNoiDung.Value = "";
                        this.chbTinHot.Checked = false;
                        this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Thêm mới thành công')</script>");
                    }
                    else
                    {
                        if (Request.QueryString["action"].ToString().Equals("edit"))
                        {
                            if (Request.QueryString["ID"] != null)
                            {
                                int id = Convert.ToInt32(Request.QueryString["ID"]);
                                news.Id = id;
                                if (this.fuFile.HasFile)
                                {
                                    clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + this.hfHinhAnh.Value);
                                    filename = clsBSFileUpload.SaveFile(this.fuFile, Server.MapPath("~/Images/HinhAnh/"));
                                    news.HinhAnh = filename;
                                    if (Request.QueryString["typeid"] != null)
                                    { 
                                        news.TypeID=Convert.ToInt32((Request.QueryString["typeid"]));
                                    }
                                }
                                obj.Update(news);
                                this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "show", "<script type='text/javascript'>alert('Cập nhật thành công')</script>");
                                news = obj.GetById(id);
                                this.txtTieuDe.Text = news.TieuDe;
                                this.txtGhiChu.Text = news.GhiChu;
                                this.txtNoiDung.Value = news.NoiDung;
                                this.chbTinHot.Checked = news.Hot;
                                this.imgHinhAnh.ImageUrl = "~/Images/HinhAnh/" + news.HinhAnh;
                                this.hfHinhAnh.Value = news.HinhAnh;
                            }
                        }
                    }
                }
                this.odsNews.DataBind();
                this.GridView1.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("Xoa"))
                {
                    clsNews news = new clsNews();
                    news = obj.GetById(Convert.ToInt32(e.CommandArgument));
                    clsBSFileUpload.DeleteFile(Server.MapPath("~/Images/HinhAnh/") + news.HinhAnh);
                    obj.Delete(Convert.ToInt32(e.CommandArgument));
                    this.GridView1.DataBind();
                }
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}